package cn.jhc.Adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import cn.jhc.layout.MyImageViewTouchBase;
import cn.jhc.service.HelpTopicDataService;
import cn.jhc.service.ImageDown;
import cn.jhc.service.OnImageDownload;
import cn.jhc.utils.URL;

import com.example.shilehui.R;

public class HelpTopicDetailGalleryAdapter extends BaseAdapter {
	
	private Context _context;
	private List<String> _t2ImageDescription;
	private List<String> _t2ImageUrl;
	private Holder _holder;
	private ImageDown _imageDown;
	private Activity _activity;
	private int _width;
	private int _height;
	private Gallery _gallery;
	private Bitmap _bitmap;
	private List<ImageInfo> _imageInfoList=new ArrayList<HelpTopicDetailGalleryAdapter.ImageInfo>();
	private ImageInfo _imageInfo;
	 

    private TextView _textview;
	

	
	public HelpTopicDetailGalleryAdapter(Context context,Gallery gallery){
		
		_gallery=gallery;
		_context=context;
		ini();
		
	}
	
	
	private void ini(){
		  
		_activity=HelpTopicDataService.getInstance().getActivity();
		
		_t2ImageDescription=HelpTopicDataService.getInstance().getT2ImageDescription();
		_t2ImageUrl=HelpTopicDataService.getInstance().getT2ImageUrl();
		
		for(int i=0;i<_t2ImageDescription.size();i++){
			_imageInfo=new ImageInfo(_t2ImageUrl.get(i), _t2ImageDescription.get(i));
			
				_imageInfoList.add(_imageInfo);
			
		}
		WindowManager wm = (WindowManager) _context
				.getSystemService(Context.WINDOW_SERVICE);
		_width = wm.getDefaultDisplay().getWidth();
		_height = wm.getDefaultDisplay().getHeight();
		_textview=(TextView)_activity.findViewById(R.id.ht_gallery_textview);
		
		
	}
	@Override
	public int getCount() {
		
		return _imageInfoList.size();
		
	}

	@Override
	public Object getItem(int position) {
		return _imageInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		String url;
		if(convertView==null){
			_holder=new Holder();
			convertView=new MyImageViewTouchBase(_context,_width,_height);
			_holder.imageView=(MyImageViewTouchBase) convertView;	
			_holder.imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));	
			convertView.setBackgroundColor(Color.BLACK);
	        convertView.setTag(_holder);
		}else{
			 _holder=(Holder)convertView.getTag();
		}
	
		
		url=_imageInfoList.get(position).getUrl();
		_holder.imageView.setTag(url);
		
		
		if(_imageDown==null){
			_imageDown=new ImageDown();
		} 
		

		_imageDown.imageDownload1(url, _holder.imageView, URL.imageLoadingFileURL,
				_activity, new OnImageDownload() {
					@Override
					public void onDownloadSucc(Bitmap bitmap, String c_url,
							ImageView mimageView) {
						
						
						ImageView imageView = (ImageView) _gallery
								.findViewWithTag(c_url);
						if (imageView != null) {
							
							
							imageView.setImageBitmap(bitmap);
							imageView.setTag("");
						}
					}
				});
		
		return convertView;
	}
	
	private class Holder{
		MyImageViewTouchBase imageView;
	
		
	}
	
	private class ImageInfo{
		private String url;
		private String text;
		
		public ImageInfo(String url,String text){
			this.url=url;
			this.text=text;
		}
		
		protected String getUrl(){
			return url;
		}
		
		protected String getText(){
			return text;
		}
	}
	
	
	
}
